package com.motion.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.motion.dto.AdminActionDto;
import com.motion.dto.AdminCourseDto;
import com.motion.generator.entity.Action;
import com.motion.generator.entity.Course;
import com.motion.generator.mapper.ActionMapper;
import com.motion.generator.mapper.CourseMapper;
import com.motion.generator.mapper.UserMapper;
import com.motion.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ActionService {
    @Autowired(required = false)
    private ActionMapper actionMapper;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private CourseService courseService;
    public Action getActionById(Long id){
        return actionMapper.selectById(id);
    }

    public IPage<Action> getPageAll(Long page, Long size){
        return actionMapper.selectPage(new Page<Action>(page,size),new QueryWrapper<>());
    }
    public IPage<Action> getPageByStrall(Long page, Long size ,int type, LocalDateTime starTime, LocalDateTime endTime, String searchStr){
        QueryWrapper<Action> queryWrapper=new QueryWrapper<>();
        if (type!=2)
            queryWrapper.eq("type",type);
        if(starTime!=null)
            queryWrapper.ge("createTime",starTime);
        if (endTime!=null)
            queryWrapper.le("createTime",endTime);
        if (!searchStr.equals(""))
            queryWrapper.like("actionName",searchStr);
        return actionMapper.selectPage(new Page<Action>(page,size),queryWrapper);
    }

    public List<AdminActionDto> actionList2AdminActionDtoLsit(List<Action> actionList){
        List<AdminActionDto> refuse=new ArrayList<>();
        for (Action item:actionList){
            AdminActionDto tem=new AdminActionDto();
            tem.setActionId(item.getActionId());
            tem.setActionImgs(item.getActionImgs());
            tem.setActionName(item.getActionName());
            tem.setDuration(item.getDuration());
            if(item.getType()==1)
                tem.setType("计次");
            else{
                tem.setType("计时");
            }
            tem.setUploadtime(item.getCreateTime());
            tem.setCreator(userMapper.selectById(item.getUploadId()).getNickName());
            refuse.add(tem);
        }
        return refuse;
    }
    public Action insertActionDto(Action action){
        actionMapper.insert(action);
        return action;
    }
    public boolean deleteAction(Long actionId){
        actionMapper.deleteById(actionId);
        return true;
    }
    public Action updataAction(Action action){
        actionMapper.updateById(action);
        return actionMapper.selectById(action);
    }

    //根据字符选择动作
    public IPage<Action> getPageByStr(String str,Long page, Long size){
        QueryWrapper<Action> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("actionName",str);
        queryWrapper.orderByDesc("createTime");
        return actionMapper.selectPage(new Page<Action>(page,size),queryWrapper);
    }

    public static File MultipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 若需要防止生成的临时文件重复,可以在文件名后添加随机码

        try {
            File file = File.createTempFile(fileName, prefix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //动作联动提醒
    public List<Course> getDelectActionToCourse(@RequestParam Long actionId) {
        List<Course> refuse = new ArrayList<>();
        //减少消耗 第一次筛选 使用sql
        List<Course> courseList = courseService.actionIdToCourse_imperfect(actionId);
        //第二次 筛选
        for (Course item : courseList) {
            String[] stringList = item.getActions().split(";");
            for (String item2 : stringList) {
                if (item2.equals(String.valueOf(actionId))) {
                    refuse.add(item);
                    break;
                }
            }
        }
        return refuse;
    }
}
